package open.tech.dawn.rest.module.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import open.tech.dawn.rest.common.annot.Menu;
import open.tech.dawn.rest.common.page.PageFactory;
import open.tech.dawn.rest.common.page.PageInfo;
import open.tech.dawn.rest.module.system.model.SysLoginLog;
import open.tech.dawn.rest.module.system.query.LoginLogQuery;
import open.tech.dawn.rest.module.system.service.ISysLoginLogService;
import open.tech.dawn.web.core.aop.Permission;
import open.tech.dawn.web.core.controller.BaseController;
import open.tech.dawn.web.core.support.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 登录记录 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/system/sysLoginLog")
@Menu(name = "登录日志", icon = "fa fa-history", code = "loginLog", url = "/views/system/log/login_log", order = 4, page = true, parentCode = "system")
public class SysLoginLogController extends BaseController {

    @Autowired
    private ISysLoginLogService loginLogService;


    /**
     * 查询登录日志列表
     */
    @RequestMapping(value = "/list", method = {RequestMethod.POST})
    @Permission
    @Menu(name = "查询登录日志列表", code = "loginLog_list", parentCode = "loginLog")
    public Object list(@RequestBody LoginLogQuery query) {
        Page<SysLoginLog> page = new PageFactory<SysLoginLog>().create(query);
        List<SysLoginLog> result = loginLogService.getLoginLogs(page, query.getBeginTime(), query.getEndTime(), query.getLogName());
        page.setRecords(result);
        return new PageInfo<>(page);
    }

    /**
     * 清空日志
     */
    @RequestMapping(value = "/delLoginLog", method = {RequestMethod.GET})
    @Permission
    @Menu(name = "清空日志", code = "loginLog_delLoginLog", parentCode = "loginLog")
    public Object delLog() {
        loginLogService.remove(new QueryWrapper<>());
        return Result.createSuccess();
    }

}
